package com.zzzzzz.oms.shipment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Validator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zzzzzz.core.datagrid.BaseData;
import com.zzzzzz.oms.driver.Driver;
import com.zzzzzz.oms.driver.DriverDao;
import com.zzzzzz.oms.legs.Legs;
import com.zzzzzz.oms.legs.LegsDao;
import com.zzzzzz.oms.legs.LegsService;
import com.zzzzzz.oms.temporary.Temporary;
import com.zzzzzz.oms.temporary.TemporaryDao;
import com.zzzzzz.oms.vehicle.Vehicle;
import com.zzzzzz.oms.vehicle.VehicleDao;
import com.zzzzzz.plugins.shiro.I;
import com.zzzzzz.sys.platform.Platform;
import com.zzzzzz.sys.platform.PlatformDao;
import com.zzzzzz.utils.DateTimeUtils;

@Service
public class ShipmentService {
	@Resource
	public ShipmentDao shipmentDao;
	
	@Resource
	public Validator validator;
	
	@Resource
	public PlatformDao platformDao;
	
	@Resource
	public LegsDao legsDao;
	
	@Resource
	public VehicleDao vehicleDao;
	
	@Resource
	public DriverDao driverDao;
	
	@Resource
	public LegsService legsService;
	
	@Resource
	public TemporaryDao temporaryDao;
	
	// 增加，修改
	@Transactional
	public synchronized Long  save(Shipment shipment, I i) {
		shipment.setUpdDt(new Date());
		shipment.setUpdBy(i.getId());
		if (shipment.getId() == null) {
			shipment.setAddDt(new Date());
			shipment.setAddBy(i.getId());
			shipment.setPlatformId(i.getPlatformId());
			shipment.setStatus("shipmentEd");
			if(shipment.getTempShipmentId()!=null){
				List<Legs> list=legsDao.findByTempShipmentId(shipment.getTempShipmentId());
				shipment.setFtranslocationId(list.get(0).getFtranslocationId());
				shipment.setTtranslocationId(list.get(0).getTtranslocationId());
			}
			if(shipment.getVehicleId()!=null){
				Vehicle vehicle=vehicleDao.findById(shipment.getVehicleId());
				shipment.setCarNo(vehicle.getVehicleNo());
				shipment.setVehicleTypeId(vehicle.getVehicleTypeId());
			}
			if(shipment.getDriverId()!=null){
				Driver driver=driverDao.findById(shipment.getDriverId());
				shipment.setDriverName(driver.getName());
				shipment.setPhone(driver.getPhone());
				shipment.setIdcard(driver.getIdcard());
			}
			Platform platform=platformDao.findById(i.getPlatformId());
			SimpleDateFormat dateformat=new SimpleDateFormat("yyyyMMdd");
			String date=dateformat.format(new Date());
			Temporary temporary=new Temporary();
			try {
				temporary=temporaryDao.findTemporary(i.getPlatformId());
				if(!temporary.getCd().equals(platform.getCd()+date)){
					temporary.setCd(platform.getCd()+date);
					temporary.setVal(1);
					temporary.setPlatformId(i.getPlatformId());
					shipment.setCd(platform.getCd()+date+"0001");
				}else{
					if(temporary.getVal()<=9){
						shipment.setCd(platform.getCd()+date+"000"+String.valueOf(temporary.getVal()));
					}else if(9<temporary.getVal()&&temporary.getVal()<=99){
						shipment.setCd(platform.getCd()+date+"00"+String.valueOf(temporary.getVal()));
					}else if(99<temporary.getVal()&&temporary.getVal()<=100){
						shipment.setCd(platform.getCd()+date+"0"+String.valueOf(temporary.getVal()));
					}else{
						shipment.setCd(platform.getCd()+date+String.valueOf(temporary.getVal()));
					}
				}
			} catch (Exception e) {
				temporary.setCd(platform.getCd()+date);
				temporary.setVal(1);
				temporary.setPlatformId(i.getPlatformId());
				temporaryDao.add(temporary);
				shipment.setCd(platform.getCd()+date+"0001");
			}
			temporaryDao.updById(temporary.getCd(),temporary.getVal()+1, i.getPlatformId());
			Long id=shipmentDao.add(shipment);
			return id;
		} else {
			shipmentDao.updById(shipment);
		}

		return (long) 1;
	}
	
	// 车辆跟踪
	public void updStatusByIds(String time, String status, List<Long> shipmentIds, I i){
		List<Long> ids = new ArrayList<Long>();
		legsDao.updTime(time, status, ids, shipmentIds, i);
		shipmentDao.updStatusByIds(shipmentIds, status, i);
		if(status.equals("onway") || status.equals("arrive")){
			shipmentDao.updTime(time, status, shipmentIds, i);
		}
		legsService.findByShipmentId(shipmentIds, status, i);
	}
	
	// 导入，主要是用于更新一口价
	@Transactional
	public List<BaseData> batchAdd(List<Shipment> list, I i) {
		logger.info("shipment batchAdd validate start:" + DateTimeUtils.date2Str(new Date(), DateTimeUtils.FORMAT_YYYY_MM_DD_HH_MM_SS));
		List<BaseData> listMsg=new ArrayList<BaseData>();
		int count=0;
		for (Shipment shipment : list) {
			System.out.println(shipment.getExpense());
			if(shipment.getExpense()!=null&&!shipment.getExpense().equals("")){
				count++;
			}
		}
		BaseData baseData = new BaseData();
		baseData.setErrMsg("总共有"+list.size()+"行记录；共有"+count+"行记录更新了一口价；共有"+(list.size()-count)+"行记录没有一口价；");
		listMsg.add(baseData);
		logger.info("legs batchAdd save end:" + DateTimeUtils.date2Str(new Date(), DateTimeUtils.FORMAT_YYYY_MM_DD_HH_MM_SS));
		return listMsg;
	}

	private static final Logger logger = LoggerFactory.getLogger(Shipment.class);
}
